import subprocess
from flask import Flask, request, jsonify
from flask_cors import CORS
from alg1 import alg1

app = Flask(__name__)
cors = CORS(app, resources={"*": {"origins": "*"}})

@app.route('/hello', methods=['GET'])
def hello():
    return jsonify({'message': 'Hello World!'})

@app.route('/run', methods=['POST', 'GET'])
def run():
    if (request.method == 'POST'):
        data = request.json
        alg1(int(data.get('times')))
        return jsonify({'message': 'end'})
    elif (request.method == 'GET'):
        data = request.args
        alg1(int(data.get('times')))
        return jsonify({'message': 'end'})

@app.route('/exec', methods=['GET'])
def exec():
    result = subprocess.run(['python', 'alg1.py'], capture_output=True, text=True)
    print('标准输出')
    print(result.stdout)
    
    print('标准错误')
    print(result.stderr)

    print('返回码', result.returncode)

    return jsonify({'message': 'end'})

if __name__ == '__main__':
    app.run(debug=True)
    